{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def logger(x, log_str=\"log is:\\n\"):\n",
    "    def logger_func(f):\n",
    "        res = f(x)\n",
    "        print_res = log_str + f\"\"\"{f.__name__}'s result is {x} => {res}\\n\"\"\"\n",
    "        return (print_res, logger(res, print_res))\n",
    "    return logger_func\n",
    "\n",
    "def get_logging(logger_tuple): return logger_tuple[0]\n",
    "def get_logger(logger_tuple): return logger_tuple[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x): return x + 1\n",
    "def g(x): return x * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "log is:\n",
      "f's result is 1 => 2\n",
      "g's result is 2 => 4\n",
      "f's result is 4 => 5\n",
      "f's result is 5 => 6\n",
      "g's result is 6 => 12\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "def apply_list(x, f_list):\n",
    "    return reduce(lambda l_r, f: l_r[1](f), f_list, (\"log is:\\n\", logger(x)))\n",
    "\n",
    "res1 = apply_list(1, [f, g, f, f, g])\n",
    "print(get_logging(res1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "log is:\n",
      "f's result is 1 => 2\n",
      "h's result is 2 => 4\n",
      "g's result is 4 => 8\n",
      "h's result is 8 => 64\n",
      "f's result is 64 => 65\n",
      "h's result is 65 => 4225\n",
      "f's result is 4225 => 4226\n",
      "h's result is 4226 => 17859076\n",
      "g's result is 17859076 => 35718152\n",
      "h's result is 35718152 => 1275786382295104\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def map_apply_list(g):\n",
    "    def helper(x, f_list):\n",
    "        return reduce(lambda l_r, f: l_r[1](f)[1](g), f_list, (\"log is:\\n\", logger(x)))\n",
    "    return helper\n",
    "\n",
    "def h(x): return x ** 2\n",
    "\n",
    "apply_h_after_every_run = map_apply_list(h)\n",
    "res2 = apply_h_after_every_run(1, [f, g, f, f, g])\n",
    "print(get_logging(res2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "def flat_map_apply_list(g):\n",
    "    def helper(x, f_list):\n",
    "        return reduce(lambda l_r, f: l_r[1](f)[1](g)[1], f_list, (\"log is:\\n\", logger(x)))\n",
    "    return helper\n",
    "\n",
    "def h_flat(x): return lox ** 2"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b00d0533784e0bd7ec39dc1f12727fcf9021e9a985583666278f4fb55e7dfcf4"
  },
  "kernelspec": {
   "display_name": "Python 3.10.0 64-bit ('fppy2': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
